<?php

namespace App\Http\Controllers\Users;

use App\Http\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    // 个人设置页面
    public function setting()
    {
        $user = Auth::user();
        return view('user.setting', compact('user'));
    }

    // 个人设置行为
    public function settingStore(Request $request)
    {
        // 验证
        $rules = [
            'name' => 'required|min:4',
        ];
        $this->validate($request, $rules);

        // 保存
        /* @var $user User */
        $user = Auth::user();
        $name = $request->input('name');
        if ($name != $user->name) {
            if (User::where('name', $name)->count() > 0) {
                // 已经有人注册
                return back()->withErrors(['name' => '用户名称已经被注册']);
            }
            $user->name = $name;
        }

        if ($request->file('avatar')) {
            // 获取和验证文件
            $file = $request->file('avatar');
            if ($file->getError() > 0) {
                return back()->withErrors(['message' => $file->getErrorMessage()]);
            }

            // 存储并获得路径
            // 921122b7c7d42d7b1afb3758eef1a545/zpQVdso85yYuGLK3kU1g5kvGrreYRcDk3OVbsjd1.jpeg
            $path = $file->storePublicly(md5($file->getBasename() . microtime(true)));
            $user->avatar = '/storage/' . $path;
        }

        $user->save();

        return back();
    }

    // 个人中心
    public function show(User $user)
    {
        // 这个人的信息[关注,粉丝,文章数]
        $user = User::withCount(['stars', 'fans', 'posts'])->find($user->id);

        // 文章列表(前十条,take)
        $posts = $user->posts()->latest()->take(10)->get();

        // 关注的用户(关注,粉丝,文章数)
        /* @var $stars \Illuminate\Support\Collection */
        $stars = $user->stars;
        $susers = User::whereIn('id', $stars->pluck('star_id'))->withCount(['stars', 'fans', 'posts'])->get();

        // 粉丝用户(关注,粉丝,文章数)
        /* @var $fans \Illuminate\Support\Collection */
        $fans = $user->fans;
        $fusers = User::whereIn('id', $fans->pluck('fan_id'))->withCount(['stars', 'fans', 'posts'])->get();

        return view('user.show', compact('user', 'posts', 'susers', 'fusers'));
    }

    public function fan(User $user)
    {
        /* @var $me User */
        $me = Auth::user();
        if ($me->hasStar($user->id)) {
            return [
                'code' => -1,
                'msg' => '您已经关注该用户',
            ];
        }
        $me->doFan($user->id);

        return [
            'code' => 0,
            'msg' => ''
        ];
    }

    public function unfan(User $user)
    {
        /* @var $me User */
        $me = Auth::user();
        if (!$me->hasStar($user->id)) {
            return [
                'code' => -1,
                'msg' => '您尚未关注该用户',
            ];
        }

        $me->doUnFan($user->id);

        return [
            'code' => 0,
            'msg' => ''
        ];
    }
}
